package com.mytrip.util;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mytrip.model.City;
import com.mytrip.model.FlightCity;
import com.mytrip.model.FlightCompany;
import com.mytrip.service.OrderManager;
import com.mytrip.service.TripManager;

public class Caches{
	private static final Log log = LogFactory.getLog(Caches.class);

	public static List<City> cityList;

	public static List<FlightCity> flightCityList;

	public static List<FlightCompany> flightCompanyList;

	public static Map<Integer, List<FlightCity>> cityPorts;

	static {
		cityPorts = new HashMap<Integer, List<FlightCity>>();

		cityList = ((TripManager) SpringUtil.getBean("tripManager")).queryAllCity();

		flightCityList = new LinkedList<FlightCity>();
		for (FlightCity fc : ((OrderManager) SpringUtil.getBean("orderManager")).queryAllFlightCity()) {
			for (City c : cityList) {
				if (fc.getCityName().contains(c.getName()) || fc.getAirportName().contains(c.getName())
						|| fc.getEnName().toUpperCase().equals(c.getNamePy().toUpperCase())) {
					flightCityList.add(fc);

					Integer cityId = c.getId();
					List<FlightCity> fcList = cityPorts.get(cityId);
					if (fcList == null) {
						fcList = new LinkedList<FlightCity>();
						cityPorts.put(cityId, fcList);
					}
					fcList.add(fc);
				}
			}
		}

		flightCompanyList = ((OrderManager) SpringUtil.getBean("orderManager")).queryAllFlightCompany();
	}
}
